package cn.hxzy.teacher.controller;


import cn.hxzy.common.domain.Direction;
import cn.hxzy.common.domain.SysSubject;
import cn.hxzy.common.domain.SysSubjectLabel;
import cn.hxzy.common.response.R;
import cn.hxzy.common.response.StatusEnum;
import cn.hxzy.teacher.service.ISysSubjectLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tyhxzy
 * @since 2024-12-14
 */
@RestController
@RequestMapping("/subjectlabel")
public class SysSubjectLabelController {
    @Autowired
    private ISysSubjectLabelService sysSubjectService;

    //    查询所有
    @RequestMapping("/selectAll")
    public R selectAll() {
        List<SysSubjectLabel> sysSubjectLabels = sysSubjectService.selectAll();
        if (sysSubjectLabels != null) {
            return R.success(sysSubjectLabels);
        }
        return R.error(400, "查询失败");
    }

    // 删除
    @RequestMapping("/deleteByID/{id}")
    public R deleteByID(@PathVariable Integer id) {
        List<SysSubject> sysSubject = sysSubjectService.selectByID(id);
        if ( sysSubject.size()>0) {
            return R.error(400, "该题目已关联，无法删除");
        }else {

            sysSubjectService.deleteByID(id);
            return R.success("删除成功");
        }
    }

    // 修改
    @RequestMapping("/updateLabel")
    public R updateLabel(@RequestBody SysSubjectLabel sysSubjectLabel) {
        int i = sysSubjectService.updateLabel(sysSubjectLabel);
        if (i > 0) {
            return R.success("修改成功");
        }
        return R.error(400, "修改失败");
    }

    // 新增
    @RequestMapping("/insertLabel")
    public R insertLabel(@RequestBody SysSubjectLabel sysSubjectLabel) {
        int i = sysSubjectService.insertLabel(sysSubjectLabel);
        if (i > 0) {
            return R.success("新增成功");
        }

        return R.error(400, "新增失败");
    }


    //查询所有
    @RequestMapping("/list")
    public R<List<SysSubjectLabel>> list(){
        List<SysSubjectLabel> selectquanbu = sysSubjectService.selectquanbu();
        if(selectquanbu!=null){
            return R.success(selectquanbu);
        }else {
            return R.error(StatusEnum.SERVICE_ERROR);
        }
    }
}
